import { BUNDLE_TYPE_ENUM, EVENT_NAME_ENUM } from "../../base/EnumIndex";
import { Tools } from "../../common/Tools";
import { AudioManager1 } from "../../manager/AudioManager1";
import { EventManager } from "../../manager/EventManager";
import { UIManager } from "../../manager/UIManager";
import LevelBase from "../../LevelBase";
import LvItem from "../../LvItem";
import LevelPre_15 from "./LevelPre_15";


const { ccclass, property } = cc._decorator;

@ccclass
export default class Level_15_194 extends cc.Component {

    level15: LevelPre_15 = null
    audioCount: number = 0;

    /** 第几层 */
    louceng = 1;

    start() {
        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        EventManager.ins.on(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);

    }

    onDisable() {
        EventManager.ins.off(EVENT_NAME_ENUM.FINISH_INTER_TWEEN, this.finish, this);
    }


    gN(NodeName: string) {
        if (!this.level15.getNodeForName(NodeName)) {
            cc.log(NodeName)
        }
        if (!this.level15) {
            this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);
        }
        if (!this.level15.getNodeForName(NodeName).getComponent(LvItem)) {
            this.level15.getNodeForName(NodeName).addComponent(LvItem);
        }
        return this.level15.getNodeForName(NodeName);
    }
    gNa(NodeName: string, active: boolean) {
        if (!this.level15.getNodeForName(NodeName)) {
            cc.log(NodeName, active)
        }
        this.level15.getNodeForName(NodeName).active = active;
    }
    zhuabu() {
        this.gN("按钮").active = false;
        switch (this.louceng) {
            case 1:
                if (this.level15.isHaveStates(["1_1", "1_2"])) {
                    // 正确
                    this.scheduleOnce(() => {
                        this.gN("shimingjie").getComponent(sp.Skeleton).setAnimation(0, "dou", true)
                        AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    }, 2)
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("right1").interTween);
                } else {
                    this.gN("shimingjie").getComponent(sp.Skeleton).setAnimation(0, "dou", true)
                    AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("wrong1").interTween);
                }
                break;
            case 2:
                if (this.level15.isHaveStates(["2_1", "2_2", "2_3"])) {
                    // 正确
                    this.scheduleOnce(() => {
                        this.gN("baimaonan").getComponent(sp.Skeleton).setAnimation(0, "dou", true)
                        AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)

                        if (this.level15.isHaveStates(["2_4"])) {
                            cc.log("2_4")
                            this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒玩具").active = false
                        }

                        if (this.level15.isHaveStates(["2_3"])) {
                            this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒缝合线").active = true

                            this.scheduleOnce(() => {
                                this.gN("baimaonan").getComponent(sp.Skeleton).setAttachment("中毒头发掀上", "中毒头发掀上")
                            }, 0.01)
                        }
                    }, 2)
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("right1").interTween);
                } else {
                    this.scheduleOnce(() => {
                        this.gN("baimaonan").getComponent(sp.Skeleton).setAnimation(0, "dou", true)
                        AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                        if (this.level15.isHaveStates(["2_4"])) {
                            this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒玩具").active = false
                        } else {
                            this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒玩具").active = true
                        }
                        if (this.level15.isHaveStates(["2_3"])) {
                            this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒缝合线").active = true
                        } else {
                            this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒缝合线").active = false
                        }
                        this.scheduleOnce(() => {
                            if (this.level15.isHaveStates(["2_3"])) {
                                this.gN("baimaonan").getComponent(sp.Skeleton).setAttachment("中毒头发掀上", "中毒头发掀上")
                            } else if (this.level15.isHaveStates(["2_2"])) {
                                this.gN("baimaonan").getComponent(sp.Skeleton).setAttachment("中毒头发掀上", "中毒顺毛")
                            } else {
                                this.gN("baimaonan").getComponent(sp.Skeleton).setAttachment("中毒头发掀上", "中毒帽子")
                            }
                        }, 0.01)
                    }, 2)
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("wrong1").interTween);
                }
                break;
            case 3:
                this.scheduleOnce(() => {
                    this.gN("pengke").getComponent(sp.Skeleton).setAnimation(0, "dou", true)
                    AudioManager1.playAudio("女中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.scheduleOnce(() => {
                        if (this.level15.isHaveStates(["3_2"])) {
                            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒脸", "中毒脸")
                        } else if (this.level15.isHaveStates(["3_1"])) {
                            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒脸", "中毒化妆脸")
                        } else {
                            this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒脸", "中毒墨镜")
                        }
                    })
                }, 2)
                this.level15.runNodeTween(this.node, this.level15.getInterTwForName("wrong1").interTween);
                break
            case 4:
                if (this.level15.isHaveStates(["4_1", "4_2", "4_3"])) {
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("right4").interTween);
                } else {
                    this.scheduleOnce(() => {
                        this.gN("niunaigong").getComponent(sp.Skeleton).setAnimation(0, "dou", true)
                        AudioManager1.playAudio("男中毒", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    }, 2)
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("wrong1").interTween);
                }
                break;
            default:
                break;
        }
        this.level15.addHaveStates([this.louceng + ""])
        this.louceng++;
        if (this.louceng < 5) {
            this.gNa(`门${this.louceng}`, true);
        }
    }
    async likai() {
        switch (this.louceng) {
            case 1:
            case 2:
                let sayInfo1 = { "node": "jingcha1say", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                await this.level15.sayNodeQipaoTween(sayInfo1);
                this.scheduleOnce(() => {
                    this.gN("jingcha").setScale(cc.v2(-1, 1))
                    AudioManager1.playAudio("信号干扰", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gNa("屋子变暗", true);
                    this.gNa("xiaohei", true);
                    this.gNa("一楼伪人", false);
                    this.gNa("二楼伪人", false);
                    this.gNa("三楼伪人", false);
                    cc.tween(this.gN("xiaohei")).set({ position: cc.v3(170, -550) }).delay(1).by(0.5, { x: -200 }).start();
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("overGame").interTween);
                }, 2)
                break;
            case 3:
                if (this.level15.isHaveStates(["3_1", "3_2", "3_3", "3_4"])) {
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("right3").interTween);
                } else {
                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("wrong3").interTween);
                }
                break;
            case 4:
                let sayInfo2 = { "node": "jingcha1say", "str": "该居民嫌疑已排除！", "audio": { "resUrl": "该居民嫌疑已排除！", "value": 1 } }
                await this.level15.sayNodeQipaoTween(sayInfo2);
                this.scheduleOnce(() => {
                    this.gN("jingcha").setScale(cc.v2(-1, 1))
                    AudioManager1.playAudio("信号干扰", 1, BUNDLE_TYPE_ENUM.LEVEL)
                    this.gNa("屋子变暗", true);
                    this.gNa("一楼伪人", false);
                    this.gNa("二楼伪人", false);
                    this.gNa("三楼伪人", false);
                    this.gNa("倒地牛奶工", false);
                    this.gNa("niunaigong", false);
                    this.gNa("weiren", true);
                    this.gN("weiren").setScale(1, 1);
                    cc.tween(this.gN("weiren")).set({ position: cc.v3(57, -341) }).delay(1).by(1, { x: -200 }).delay(1).by(1, { x: -200 }).start();
                    this.scheduleOnce(() => {
                        this.level15.runNodeTween(this.node, this.level15.getInterTwForName("error4").interTween);
                    }, 2.5)
                }, 2)
                break;
            default:
                break;
        }
        this.level15.addHaveStates([this.louceng + ""])
        this.louceng++;
        if (this.louceng < 5) {
            this.gNa(`门${this.louceng}`, true);
        }
    }

    async finish(finishStr: any, lvItem: LvItem) {
        if (typeof finishStr !== "string") { return }

        this.level15 = cc.director.getScene().getChildByName("Canvas").getComponentInChildren(LevelPre_15);

        if (finishStr === "reviveGame") {
            this.louceng--;
            console.log("复活", this.louceng);
            AudioManager1.playBG("恐怖关卡背景音乐", 1, BUNDLE_TYPE_ENUM.LEVEL)
            this.gN("duqi").opacity = 0;
            this.gNa("一楼伪人", true);
            this.gNa("二楼伪人", true);
            this.gNa("三楼伪人", true);
            this.gNa("四楼伪人", true);
            this.gNa("wrongNode", false);
            this.gNa("屋子变暗", false);
            this.gNa("按钮", false);
            this.gNa("成功", false);
            this.gNa("失败", false);
            switch (this.louceng) {
                case 1:
                    this.gN("xiaohei").setPosition(cc.v3(258, -503));
                    this.gNa("xiaohei", false);
                    this.gNa("剪刀", true);
                    this.gNa("剪刀挂", true);
                    this.gNa("l1_1", true);
                    this.gNa("l1_2", true);
                    this.gN("shimingjie").getComponent(sp.Skeleton).setAnimation(0, "daiji", true)
                    this.gN("shimingjie").getComponent(sp.Skeleton).findBone("绷带").active = true;
                    this.gN("shimingjie").getComponent(sp.Skeleton).findBone("颈托").active = true;
                    this.level15.removeStates(["1_1", "1_2", "1"]);

                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("start").interTween);
                    break;
                case 2:
                    this.gN("xiaohei").setPosition(cc.v3(258, -503));
                    this.gNa("xiaohei", false);
                    this.gNa("l2_1", true);
                    this.gNa("l2_2", true);
                    this.gNa("l2_3", false);
                    this.gNa("l2_4", true);
                    this.gN("baimaonan").getComponent(sp.Skeleton).setAnimation(0, "daiji", true)
                    this.gN("baimaonan").getComponent(sp.Skeleton).findBone("蓝色瞳孔").active = false;
                    this.gN("baimaonan").getComponent(sp.Skeleton).findBone("缝合线").active = false;
                    this.gN("baimaonan").getComponent(sp.Skeleton).findBone("中毒缝合线").active = false;
                    this.gN("baimaonan").getComponent(sp.Skeleton).findBone("玩具").active = true;
                    this.scheduleOnce(() => {
                        this.gN("baimaonan").getComponent(sp.Skeleton).setAttachment("头发掀上", "帽子");
                    })
                    this.gN("jingcha").setPosition(cc.v3(-700, -650));
                    this.level15.removeStates(["2_1", "2_2", "2_3", "2_4", "2"]);

                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("start").interTween);
                    break;
                case 3:
                    this.gNa("柜子开", false);
                    this.gNa("卸妆水", false);
                    this.gNa("l3_1", true);
                    this.gNa("l3_3", true);
                    this.gNa("l3_2_1", true);
                    this.gNa("l3_2", false);
                    this.gNa("l3_4", false);
                    this.gN("pengke").getComponent(sp.Skeleton).setAnimation(0, "daiji", true)
                    this.gN("pengke").getComponent(sp.Skeleton).setAttachment("中毒脸", "中毒墨镜");
                    this.gN("pengke").getComponent(sp.Skeleton).setAttachment("待机呲尖牙", "待机正常嘴");
                    this.gN("pengke").getComponent(sp.Skeleton).setAttachment("待机脸", "待机墨镜");
                    this.gN("jingcha").setPosition(cc.v3(-700, -650));
                    this.level15.removeStates(["3_1", "3_2", "3_3", "3"]);

                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("start").interTween);
                    break;
                case 4:
                    this.gN("weiren").setScale(0.9, 0.9);
                    this.gN("niunaigong").getComponent(sp.Skeleton).setAnimation(0, "daiji", true)
                    this.gNa("倒地牛奶工", true);
                    this.gNa("weiren", false);
                    this.gNa("niunaigong", true);
                    this.gN("niunaigong").setPosition(cc.v3(57, -341))
                    this.gN("weiren").setPosition(cc.v3(238, -278))
                    this.gNa("匕首", false);
                    this.gNa("毛巾", true);
                    this.gNa("l4_1", true);
                    this.gNa("l4_2", true);
                    this.gNa("l4_3", true);
                    this.gN("niunaigong").getComponent(sp.Skeleton).setAttachment("油漆头", "血头");
                    this.gN("jingcha").setPosition(cc.v3(-700, -650));
                    this.level15.removeStates(["4_1", "4_2", "4_3", "4"]);

                    this.level15.runNodeTween(this.node, this.level15.getInterTwForName("start2").interTween);
                    break;
            }

        }
        if (finishStr.includes("提示字")) {
            this.typewriterEffect();
        }
    }


    typewriterEffect() {
        this.schedule(this.typeNextCharacter, 1 / this.typingSpeed);
    }

    typeNextCharacter() {
        if (this.typingFinish) {
            this.unschedule(this.typeNextCharacter);
            return;
        }

        // 添加下一个字符到文本内容
        this.textLabel.getComponent(cc.Label).string += this.content[this.currentIndex];

        // 更新当前索引
        this.currentIndex++;

        // 判断是否打字完成
        if (this.currentIndex >= this.content.length) {
            this.typingFinish = true;
        }
    }

    @property(cc.Node)
    textLabel: cc.Node = null;

    @property({ displayName: "打字速度" })
    typingSpeed: number = 3; // 打字速度，每秒多少个字符

    @property({ displayName: "文本内容" })
    content: string = ""; // 完整的文本内容

    private currentIndex: number = 0; // 当前打字到的索引
    private typingFinish: boolean = false; // 打字是否完成



}
